/**
 * File:   Iterator.h
 * Author:
 *
 */

#ifndef ITERATOR_H
#define ITERATOR_H

#include "contest_interface.h"

class IteratorLink;

typedef struct Iterator {
private:
    IteratorLink *current;
    IteratorLink *first;

    Index *idx;
    Key min_key;
    Key max_key;

    bool _end;
    bool _closed;
    bool _initialized;
    bool _reading;

    bool _deleteRecords; // If true, in the destruction time Iterator should 
    // free the memory used by the records it contains

public:
    Iterator();
    Iterator(Record *_current, Index *_idx, Key _min_key, Key _max_key);
    Iterator(Index *_idx, Key _min_key, Key _max_key);
    ~Iterator();

    // Initialize the iterator
    void Init(Record *_current, Index *_idx, Key _min_key, Key _max_key);

    // Return the record to which the iterator refers
    Record * value() throw (ErrorCode);

    // Change the value pointed by current iterator
    void changeValue(Record * value);

    // Return whether the iterator has been closed

    bool closed() const {
        return _closed;
    };
    // Return whether the iterator has exceeded its range

    bool end() const {
        return _end;
    };

    bool hasNext() const {
        return !_end;
    };

    // Insert new record in iterator_link list (insertion sort)
    bool insert(Record * new_record) throw (ErrorCode);

    // Move the iterator to the next record
    bool next() throw (ErrorCode);
    bool iterate_forward() throw (ErrorCode);

    // Move the iterator to the previous record
    bool previous() throw (ErrorCode);
    bool iterate_backward() throw (ErrorCode);

    // Close the iterator
    void Close() throw (ErrorCode);
    // Mark the iterator as ended
    void SetEnded() throw (ErrorCode);

    // Set _deleteRecords = true;

    void SetDeleteRecords() {
        this->_deleteRecords = true;
    }
    // Set _deleteRecords = false;

    void UnsetDeleteRecords() {
        this->_deleteRecords = false;
    }

    // Function used to free all resources from iterator
    ErrorCode delete_iterators();

    // Functions needed for the ShadowSpace
    // Delete  given record from Iterator
    void deleteFromList(Record * originalRecord);
    // Update given record in Iterator
    void updateFromList(Record * changedRecord, Record * originalRecord);
    // Test if key between min and max
    bool testBounds(Key keyToBeTested);
} Iterator;

#endif /*ITERATOR_H*/
